package com.zdzx.controller.admin.content;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdzx.common.result.PageInfo;
import com.zdzx.common.result.PageUtil;
import com.zdzx.common.result.Result;
import com.zdzx.common.result.ResultCode;
import com.zdzx.common.result.ResultUtil;
import com.zdzx.mapper.BannerMapper;
import com.zdzx.model.service.content.vo.Banner;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "banner")
@RestController
@RequestMapping("/admin")
public class BannerController {


	@Autowired
	private BannerMapper bannerMapper;
	
	@ApiOperation(value = "获取列表", notes = "")
	@GetMapping("/banner")
	public Result<PageInfo<Banner>> list(@ApiParam("") @RequestParam(value = "pageNum", required = false) Integer pageNum,
			@ApiParam("") @RequestParam(value = "pageSize", required = false) Integer pageSize){
		Page<Banner> page = new Page<>(pageNum==null?1:pageNum, pageSize==null?10:pageSize);
		QueryWrapper<Banner> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByDesc("update_time");
		IPage<Banner> info = bannerMapper.selectPage(page, queryWrapper);
		PageInfo<Banner> data = PageUtil.data(info.getPages(),info.getCurrent(),info.getTotal(),info.getRecords());
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, data);
	}

	@ApiOperation(value = "添加", notes = "")
	@PostMapping("/banner")
	public Result<String> save(@RequestBody Banner entity){
		int result=0;
		//entity.setImagePath(entity.getImagePath().replace(SystemConfig.ossIp, ""),false);
		if(StringUtils.isEmpty(entity.getId())) {
			entity.setCreateTime(LocalDateTime.now());
			entity.setUpdateTime(LocalDateTime.now());
			result=bannerMapper.insert(entity);
		}else {
			entity.setUpdateTime(LocalDateTime.now());
			result = bannerMapper.updateById(entity);
		}
		if (result>0) {
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, entity.getId());
		} else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), ResultCode.RESULT_ERROR.getMes(), null);
		}
	}

	@ApiOperation(value = "修改", notes = "")
	@PutMapping("/banner")
	public Result<String> edit(@RequestBody Banner entity){
		int result=0;
		//entity.setImagePath(entity.getImagePath().replace(SystemConfig.ossIp, ""),false);
		if(StringUtils.isEmpty(entity.getId())) {
			result=bannerMapper.insert(entity);
		}else {
			result = bannerMapper.updateById(entity);
		}
		if (result>0) {
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
		} else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), ResultCode.RESULT_ERROR.getMes(), null);
		}
	}

	@ApiOperation(value = "删除", notes = "")
	@DeleteMapping("/banner/{bannerId}")
	public Result<String> del(@PathVariable("bannerId") String bannerId){
		int result = bannerMapper.deleteById(bannerId);
		if (result > 0) {
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
		} else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), ResultCode.RESULT_ERROR.getMes(), null);
		}
	}
	
}
